﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Koeltp.DataAccess;
using System.Data.SqlClient;

public partial class admin_Article_addarticle : System.Web.UI.Page
{
    private IDataAccess da = IDataAccessFactory.CreateDataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetArticleType();
        }
    }
    protected void iuTest_Uploaded(object sender, EventArgs e)
    {
        lbImages.Text = iuTest.PicSavedPath;
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        string introduction = tbIntroduction.Text.Trim();
        string articleTitle = tb_ArticleTitle.Text.Trim();
        String content =Server.HtmlEncode(this.txtcontent.Text.Trim());
        String articleType = ddl_ArticleType.SelectedValue.Trim();
        String articleKeyword = tbArticleKeyword.Text.Trim();
        Boolean Article_Status = true;
        Int32 Users_Id;
        if (cbSaveDraft.Checked == true)
        {
            Article_Status = false;
        }
        if (Session["Users_Id"] != null)
        {
            Users_Id = Convert.ToInt32(Session["Users_Id"]);
        }
        else
        {
            Users_Id = 1;
        }
        string sql = "insert into T_Article(Article_Introduction,Users_Id,ArticleType_Id,Article_StartTime,Article_UpdateTime,Article_Title,Article_Content,Article_Status,Article_Keyword) values(@Article_Introduction,@Users_Id,@ArticleType_Id,GETDATE(),GETDATE(),@article_title,@article_content,@Article_Status,@Article_Keyword)";
        DataTable dt = new DataTable();
        QueryParameter[] parameter = {
                                         new QueryParameter("@Article_Introduction",introduction,DbType.String),
                                         new QueryParameter("@article_title",articleTitle , DbType.String),
                                         new QueryParameter("@article_content",content, DbType.String),
                                         new QueryParameter("@ArticleType_Id",articleType ,DbType.String),
                                         new QueryParameter("@Article_Status", Article_Status, DbType.Boolean),
                                         new QueryParameter("@Users_Id", Users_Id, DbType.Int32),
                                         new QueryParameter("@Article_Keyword", articleKeyword, DbType.String)
                                     };
        int state = da.ExecuteNonQuery(sql, CommandType.Text, parameter);
        if (state == 1)
        {
            Response.Write("<script type=\"text/javascript\">alert('保存成功！')</script>");
        }
        else
        {
            Response.Write("<script type=\"text/javascript\">alert('保存失败！')</script>");
        }
    }
    /// <summary>
    /// 文章类型绑定
    /// </summary>
    protected void GetArticleType()
    {
        string sql = "select ArticleType_Id,ArticleType_Name from dbo.T_ArtitleType";
        DataTable dt = new DataTable();
        da.GetTable(sql, dt, CommandType.Text);
        ddl_ArticleType.DataSource = dt;
        ddl_ArticleType.DataTextField = "ArticleType_Name";
        ddl_ArticleType.DataValueField = "ArticleType_Id";
        ddl_ArticleType.DataBind();
    }
}

